package com.example.myapplication1;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.Nullable;

/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/9/1 9:40
 * @last-modify-date: 2020/9/1 9:40
 * @description:
 */
public class AnimationActivity extends Activity {
    private static final String TAG = "AnimationActivity:";
    private LoadingView loadingview;
    private ProgressBar progress;
    private ImageView img_ball;
    private ImageView img_color;
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        loadingview = findViewById(R.id.loadingview);
        loadingview.start();
        img_ball = findViewById(R.id.img_ball);
        img_color = findViewById(R.id.img_color);
        progress = findViewById(R.id.progress);
        //Background
        ClipDrawable bgClipDrawable = new ClipDrawable(new ColorDrawable(Color.RED), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        bgClipDrawable.setLevel(10000);
        //Progress
        ClipDrawable secondaryprogressClip = new ClipDrawable(new ColorDrawable(Color.GREEN), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        ClipDrawable progressClip = new ClipDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, ClipDrawable.HORIZONTAL);
        //Setup LayerDrawable and assign to progressBar
        Drawable[] progressDrawables = {bgClipDrawable, secondaryprogressClip/*second*/, progressClip};
        LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
        progressLayerDrawable.setId(0, android.R.id.background);
        progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
        progressLayerDrawable.setId(2, android.R.id.progress);

        img_color.setBackgroundColor(Color.RED);

        progress.setProgressDrawable(progressLayerDrawable);
        progress.setSecondaryProgress(70);
        progress.setProgress(50);
        img_ball.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                BitmapDrawable bd  = (BitmapDrawable) img_ball.getDrawable();
                Bitmap bitmap = bd.getBitmap();//获取图片的位图
                //初始化bitmap的原图宽高
                int intrinsicHeight = bitmap.getHeight();
                int intrinsicWidth = bitmap.getWidth();

                //获得imageview变换矩阵
                Matrix matrix = img_ball.getImageMatrix();
                float value[] = new float[10];
                matrix.getValues(value);

                float widthRatio = value[0];//x轴缩放系数
                float heightRatio = value[4];//y轴缩放系数

                //获取图像的边界值，根据自己的imageview 在屏幕中的位置做适当调整。
                int scaledImageOffsetX = (int) (event.getX() - (img_ball.getWidth() - intrinsicWidth*widthRatio)/2);
                int scaledImageOffsetY = (int) (event.getY() - (img_ball.getHeight() - intrinsicHeight*heightRatio)/2);

                //获取点击位置，在真实图片中的像素位置
                int x = (int) (scaledImageOffsetX/widthRatio);
                int y = (int) (scaledImageOffsetY/heightRatio);
                if (x <0) {
                    x=0;
                }
                if (y< 0) {
                    y= 0;
                }
                if (x + 120 > bitmap.getWidth()) {
                    x = bitmap.getWidth() - 120;
                }
                if (y + 120 > bitmap.getHeight()) {
                    y = bitmap.getHeight() - 120;
                }
                img_ball.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
//                image2.setAlpha(alpha);
                return false;
            }
        });

    }
}
